#! /bin/bash
set -euo pipefail

echo ">>> Step 1: 配置阿里云 CentOS7 yum 源..."
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all
yum makecache fast

echo ">>> Step 2: 更新系统..."
yum -y update

echo ">>> Step 3: 安装必要工具..."
yum install -y yum-utils device-mapper-persistent-data lvm2

echo ">>> Step 4: 添加阿里云 Docker CE 源..."
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast

echo ">>> Step 5: 安装 Docker CE..."
yum install -y docker-ce docker-ce-cli containerd.io

echo ">>> Step 6: 启动并设置开机自启..."
systemctl enable --now docker

echo ">>> Step 7: 配置阿里云镜像加速器..."
mkdir -p /etc/docker
cat > /etc/docker/daemon.json <<EOF
{
 "registry-mirrors": [
    "https://docker.m.daocloud.io",
    "https://hub-mirror.c.163.com",
    "https://mirror.baidubce.com",
    "https://your_preferred_mirror",
    "https://dockerhub.icu",
    "https://docker.registry.cyou",
    "https://docker-cf.registry.cyou",
    "https://dockercf.jsdelivr.fyi",
    "https://docker.jsdelivr.fyi",
    "https://dockertest.jsdelivr.fyi",
    "https://mirror.aliyuncs.com",
    "https://dockerproxy.com",
    "https://mirror.baidubce.com",
    "https://docker.m.daocloud.io",
    "https://docker.nju.edu.cn",
    "https://docker.mirrors.sjtug.sjtu.edu.cn",
    "https://docker.mirrors.ustc.edu.cn",
    "https://mirror.iscas.ac.cn",
    "https://docker.rainbond.cc"
    ]
}
EOF

systemctl daemon-reload
systemctl restart docker

echo ">>> Step 8: 验证 Docker 安装..."
docker --version
docker run --rm hello-world || echo "⚠️ hello-world 拉取失败，请检查网络"

echo ">>> Docker 已安装并配置完成！"